#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <err.h>
#include <libmscommon/mscommon.h>
#include <libmscommon/msnetframe.h>
#include <libmscommon/msmem.h>
#include <libmscommon/msepoll.h>
#include <libmslog/mslog.h>
#include <libmsprotocol/msprotocol.h>

#define FLAG "PTL_SAMPLE"
/*
	{"control_cmd":"msifconfig","workmode":"nonework","iface_info":[{"name":"p2p1","address":"12:sdffsdfsdf","ipaddress":"192.168.0.222","netmask":"255.255.0.0","gateway":"192.168.0.1","mtu":"150","dns1":"dnndndd","dns2":"2222222","mode":"static"}]}
	{"control_cmd":"msifconfig","workmode":"nonework","iface_info":[{"name":"bond0","ipaddress":"192.168.0.222","netmask":"255.255.0.0","mode":"bond-master"},{"name":"p2p1","mode":"bond-slaver","bondmaster_name":"bond0"},{"name":"p6p1","mode":"bond-slaver","bondmaster_name":"bond0"}]}
	{"control_cmd":"mspppoe","workmode":"nonework","usage":"setup","ethn":"p2p1","user":"sugao_pppoe1","passwd":"123456"}
	{"control_cmd":"mspppoe","workmode":"nonework","usage":"start","ethn":"p2p1"}
	{"control_cmd":"mspppoe","workmode":"nonework","usage":"setup","ethn":"p6p1","user":"sugao_pppoe2","passwd":"123456"}
	{"control_cmd":"mspppoe","workmode":"nonework","usage":"start","ethn":"p6p1"}
	{"control_cmd":"mspppoe","workmode":"nonework","usage":"setup","ethn":"p6p2","user":"sugao_pppoe3","passwd":"123456"}
	{"control_cmd":"mspppoe","workmode":"nonework","usage":"start","ethn":"p6p2"}
	{"control_cmd":"mspppoe","workmode":"nonework","usage":"stop","ethn":"p6p1"}
	{"control_cmd":"mspppoe","workmode":"nonework","usage":"status","ethn":"p6p1"}

	{"control_cmd":"mspppoe","workmode":"nonework","usage":"setup","ethn":"eth2","user":"sugao_pppoe2","passwd":"123456"}
	{"control_cmd":"mspppoe","workmode":"nonework","usage":"start","ethn":"eth2"}
	{"control_cmd":"mspppoe","workmode":"nonework","usage":"stop","ethn":"eth2"}
	{"control_cmd":"mspppoe","workmode":"nonework","usage":"status","ethn":"eth2"}

	{"control_cmd":"mspppoe","workmode":"nonework","usage":"setup","ethn":"eth1","user":"sugao_pppoe3","passwd":"123456"}
	{"control_cmd":"mspppoe","workmode":"nonework","usage":"start","ethn":"eth1"}
	{"control_cmd":"mspppoe","workmode":"nonework","usage":"stop","ethn":"eth1"}
	{"control_cmd":"mspppoe","workmode":"nonework","usage":"status","ethn":"eth1"}

	{"control_cmd":"info"}
	{"control_cmd":"info","workmode":"transmit"}
	{"control_cmd":"info","task_id":1010}
	{"control_cmd":"get_state"}
	{"control_cmd":"read"}
	{"control_cmd":"clear_data"}
	{"control_cmd":"save"}
	{"control_cmd":"quit"}
	{"control_cmd":"setsysconfig","uxp_log_opt":53}
	{"control_cmd":"osinfo"}
	{"control_cmd":"rebootserver"}
	{"control_cmd":"poweroff"}
	{"control_cmd":"sysperforminfo"}
	{"control_cmd":"add","workmode":"streammonitor","listurlin":"rtp://192.168.1.244:8881"}
	{"control_cmd":"streaminfo","workmode":"nonework","listurlin":"http://192.168.200.199:8060/out1/out1.m3u8"}
	{"control_cmd":"keepalive","workmode":"nonework"}
	{"control_cmd":"serverversion"}
	{"control_cmd":"authorization"}
	{"control_cmd":"getsysconfig"}
	{"control_cmd":"add","workmode":"transmit","task_id":2001,"listurlin":"http://192.168.200.199:8060/out1/out1.m3u8","listurlout":"udp://127.0.0.1:4000","interfacein":"p2p1"}
	{"control_cmd":"add","workmode":"transmit","task_id":2002,"listurlin":"http://192.168.200.199:39100/ch1.ts","listurlout":"out_test.m3u8"}
	{"control_cmd":"delete","task_id":2002}
	{"control_cmd":"add","workmode":"transmit","task_id":1003,"listurlin":"udp://239.93.0.111:5140","listurlout":"/usr/local/nginx/html/out3.m3u8"}
	{"control_cmd":"add","workmode":"transmit","task_id":1004,"listurlin":"udp://239.93.0.71:5140","listurlout":"udp://127.0.0.1:2000","interfacein":"p2p1"}
	{"control_cmd":"add","workmode":"transmit","task_id":1007,"listurlin":"udp://239.93.0.70:5140","listurlout":"/usr/local/nginx/html/out7.m3u8"}
	{"control_cmd":"add","workmode":"transcodevideo","listurlin":"http://192.168.200.199:8010/ch1.ts","listurlout":"udp://127.0.0.1:3000"}
	{"control_cmd":"add","workmode":"transcodevideo","listurlin":"udp://224.2.2.2:3001","listurlout":"udp://224.2.2.2:2000"}
	{"control_cmd":"add","workmode":"dump_nor","task_id":123,"listurlin":"udp://224.3.3.2:10002"}
	{"control_cmd":"add","workmode":"transmit","task_id":3000,"listurlin":"file:///usr/local/mscore_web/upload/video/msatartup.mp3","listurlout":"udp://127.0.0.1:20000","interfaceout":"eth0","service_id":1230,"transport_stream_id":1240,"original_network_id":1250,"pmt_start_pid":1260,"mpegts_start_pid":1270}
	{"control_cmd":"delete","task_id":1007}
	{"control_cmd":"add","workmode":"streammonitor","listurlin":"udp://224.2.2.2:2000"}
	{"control_cmd":"add","workmode":"httpserver","listurlin":"ch1.ts-udp://224.2.2.2:3001"}
	{"control_cmd":"add","workmode":"transmit","listurlin":"http://192.168.200.199:39000/ch1.ts","listurlout":"out2.m3u8"}

	{"control_cmd":"delete","task_id":100}
	{"control_cmd":"add","workmode":"dump_timeshift_auto","task_id":1200,"pid":1200,"listurlin":"udp://239.93.0.112:5140"}
	{"control_cmd":"add","workmode":"dump_nor","listurlin":"udp://192.168.7.150:10001"}
	{"control_cmd":"add","workmode":"transmit","listurlin":"http://118.122.88.230:5460//luzhi1/vdj/live/c001/now/live.m3u8","listurlout":"udp://127.0.0.1:3000"}
	{"control_cmd":"add","workmode":"hrserver","task_id":100,"listurlin":"ch1.ts-udp://127.0.0.1:20000#ch2.ts-udp://127.0.0.1:20000","port":8010}
	{"control_cmd":"add","workmode":"srtserver","task_id":1000,"listurlin":"ch1.ts-udp://127.0.0.1:6001","port":8010}
	{"control_cmd":"delete","task_id":100}
	{"control_cmd":"add","workmode":"remux","listurlin":"rtmp://live.hkstv.hk.lxdns.com/live/hks","listurlout":"udp://127.0.0.1:3000"}
	{"control_cmd":"add","workmode":"remux","listurlin":"udp://224.2.2.2:3000","listurlout":"/usr/local/nginx/html/1.m3u8"}
	{"control_cmd":"add","workmode":"transcodevideo","listurlin":"udp://127.0.0.1:2003","listurlout":"udp://192.168.200.244:10000","task_id":100}
	{"control_cmd":"add","workmode":"transcodevideo","listurlin":"udp://224.2.2.2:2001","listurlout":"udp://127.0.0.1:3001"}
	{"control_cmd":"add","workmode":"transcodevideo","listurlin":"udp://224.2.2.2:2002","listurlout":"udp://127.0.0.1:3002"}

	
	{"control_cmd":"add","workmode":"transcodevideo","listurlin":"udp://224.2.2.2:3000","listurlout":"udp://224.2.2.3:5000","drawtext":{"enable":1,"displaypostion":1,"x":0,"y":500,"speed":40,"displaymode":0,"count":3,"interval":240,"valid_date":{"start_date":"2017-04-13","end_date":"2017-05-13","valid_time":[{"start_time":"00:00","end_time":"00:12"}],"opt":1},"box":{"enable":1,"boxborderw":0,"boxcolor":"black","line_spacing":0,"borderw":0,"bordercolor":"black"},"text":{"fontcolor":"white","font":"Sans","alpha":1,"fontsize":20,"shadowcolor":"black","shadowx":0,"shadowy":0,"content":"hello,world"}},"overlay":[{"index":0,"src":"/etc/mscore/mscore_logo.png","isscale_src":0,"forcew":100,"forceh":100,"displaypostion":0,"x":10,"y":10,"displaymode":0,"count":3,"show_duration":20,"interval":240,"valid_date":{"start_date":"2017-04-13","end_date":"2017-05-13","valid_time":[{"start_time":"00:00","end_time":"00:12"}],"opt":1}}],"videoenc":{"preset":"fast","profile":"main","level":"auto","refs":3,"gop_size":25,"max_b_frames":4,"force_idr":0,"rc":"vbr","bit_rate":2097152,"rc_buffer_size":2097152,"rc_min_rate":2097152,"rc_max_rate":2097152,"width":-1,"height":-1}}
	{"control_cmd":"add","workmode":"hrserver","task_id":1200,"port":8013,"listurlin":"ch1-udp://224.2.2.2:3000#ch2-udp://224.2.2.2:3004#ch3-udp://224.2.2.2:3008#ch4-udp://224.2.2.2:3012#ch5-udp://224.2.2.2:3016#ch6-udp://224.2.2.2:3020"}
	{"control_cmd":"delete","workmode":"hrserver","task_id":1200}

	{"control_cmd":"setauthorization","workmode":"nonework","authent_envcodecid":1,"authent_envcodectype":1}
	
	{"control_cmd":"add","workmode":"hrserver","task_id":100,"port":8013,"listurlin":"ch2222.ts-udp://224.2.2.2:12343333"}
	{"control_cmd":"add","workmode":"hrserver","task_id":100,"port":8013,"listurlin":"ch3.ts-udp://224.2.2.2:12343333"}
	{"control_cmd":"add","workmode":"hrserver","task_id":100,"port":8013,"listurlin":"ch4.ts-udp://224.2.2.2:1234"}
	{"control_cmd":"delete","workmode":"hrserver","task_id":100,"port":8013,"listurlin":"ch111.ts-udp://224.2.2.2:1234"}
	{"control_cmd":"delete","workmode":"hrserver","task_id":100,"port":8013,"listurlin":"ch2222.ts-udp://224.2.2.2:12343333"}
	{"control_cmd":"delete","workmode":"hrserver","task_id":100,"port":8013,"listurlin":"ch3.ts-udp://224.2.2.2:12343333"}
	{"control_cmd":"delete","workmode":"hrserver","task_id":100,"port":8013,"listurlin":"ch4.ts-udp://224.2.2.2:1234"}
	{"control_cmd":"add","workmode":"streammonitor","listurlin":"udp://224.2.2.2:2000"}	
	{"control_cmd":"add","workmode":"dump_nor","listurlin":"udp://192.168.7.150:10001","programname":"mscore001","platform_id":1,"task_id":101,"tasktime":"B20190731104337E20190731134337"}
	{"control_cmd":"add","workmode":"transmit","listurlin":"rtsp://111.48.37.88:554/HBUT_RTSP/00000001000000050000000000000058?Type=tv&UserID=F7163936717B01","listurlout":"udp://127.0.0.1:3000","opt":"usefifo"}

	{"control_cmd":"taskadd","workmode":"transcode","platform_id":100,"task_id":2001,"listurlin":"http://127.0.0.1:8060/mscore/av/msatartup.mp3","listurlout":"udp://127.0.0.1:3000"}
	{"control_cmd":"taskadd","workmode":"transcode","platform_id":102,"task_id":2002,"listurlin":"http://127.0.0.1:8060/mscore/av/msatartup.mp3","listurlout":"udp://127.0.0.1:3010"}
	{"control_cmd":"taskadd","workmode":"transcode","platform_id":103,"task_id":2003,"listurlin":"http://127.0.0.1:8060/mscore/av/msatartup.mp3","listurlout":"udp://127.0.0.1:3020"}
	{"control_cmd":"taskdelete","platform_id":100,"task_id":2001}
	{"control_cmd":"taskrestart","platform_id":100,"task_id":2001}
	{"control_cmd":"taskinfo"}
	{"control_cmd":"taskptid"}
	{"control_cmd":"taskinfo","platform_id":102,"task_id":2002}
	{"control_cmd":"taskinfo"}
	{"control_cmd":"taskptid"}
	{"control_cmd":"taskclear"}
	{"control_cmd":"save"}
	{"control_cmd":"taskinfocmd"}
	{"control_cmd":"getoptinfo"}
	{"control_cmd":"getcdninfo"}
	{"control_cmd":"getworkmodeinfo"}
	{"control_cmd":"factory"}
	{"control_cmd":"rebootserver"}
	{"control_cmd":"msifconfig"}
	{"control_cmd":"login"}
	{"control_cmd":"getsystime"}
	{"control_cmd":"getstate"}
	{"control_cmd":"getsysconfig"}
	{"control_cmd":"getosinfo"}
	{"control_cmd":"getsysperforminfo"}
	{"control_cmd":"getserverversion"}
	{"control_cmd":"getauthorization"}
	{"control_cmd":"getworkmodeinfo"}
*/
int msprotocol_api_mctu(int argc, char **argv)
{
	URLContext cilent_ctt;
	memset(&cilent_ctt,0,sizeof(URLContext));
	int ptltype=0;
	if(argc==3){
		ms_strcpy(cilent_ctt.url_info.hostname,argv[1]);
		cilent_ctt.url_info.port=ms_atoi(argv[2]);
	}else{
		ms_waring("Use deafult val: %s 127.0.0.1 8020",argv[0]);
		ms_strcpy(cilent_ctt.url_info.hostname,"127.0.0.1");
		cilent_ctt.url_info.port=8020;
	}
	URLProtocol *msptl=msptc_api_matchByPtcindex(msptc_http);
	ms_strcpy(cilent_ctt.opt.http.context_type,"application/json");
	cilent_ctt.opt.http.flag_noprint=ms_true;
	cilent_ctt.flags|=FLAG_WRITE;
	ms_sprintfs(cilent_ctt.url,"http://%s:%d/avsos/cmdapi/postcmd",cilent_ctt.url_info.hostname,cilent_ctt.url_info.port);

	char databuf[1024*100];
	while(1){
		ms_info("**********************************");
		ms_info("*  Input cmd    ");
		ms_info("**********************************");
		scanf("%s",databuf);

		ms_strcpy(cilent_ctt.opt.http.context,databuf);
		if(msptl->url_open(&cilent_ctt)<0){
			ms_error("Open %s failed",cilent_ctt.url);
		}else{
			ms_byte msg_reply[200*1024]={0};
			ms_s32 len_total=0;
			while(1){
				ms_s32 len=msptl->url_read(&cilent_ctt,&msg_reply[len_total],ms_bufsize(msg_reply)-len_total);
				if(len>0){
					len_total+=len;
				}else{
					break;
				}
			}
			ms_debug("%s",msg_reply);
		}
		msptl->url_close(&cilent_ctt);

	}	
}

int msprotocol_api_udp(int argc, char **argv)
{
	URLProtocol *msin_ptl=msptc_api_matchByPtcindex(msptc_file);
	URLProtocol *msout_ptl=msptc_api_matchByPtcindex(msptc_udp);
	URLContext in_ctt;
	URLContext out_ctt;
	ms_memset(&in_ctt,0,sizeof(URLContext));
	ms_memset(&out_ctt,0,sizeof(URLContext));
	ms_strcpy(in_ctt.url, "file:///home/sugao/123.ts");
	ms_strcpy(out_ctt.url, "udp://224.2.2.2:3000");
	out_ctt.opt.udp.ifcOut="lo";
	out_ctt.opt.udp.ipOut="127.0.0.1";
	out_ctt.flags|=FLAG_WRITE;
	msin_ptl->url_open(&in_ctt);
	msout_ptl->url_open(&out_ctt);
	char buf[2048]={0};
	int len=0;
	while(1){
		len=msin_ptl->url_read(&in_ctt,buf,1328);

		ms_debug("read===%d",len);
		
		msout_ptl->url_write(&out_ctt,buf,len);
		
		//ms_msleep(100);
	}	
	msin_ptl->url_close(&in_ctt);
	msout_ptl->url_close(&out_ctt);
}

int msprotocol_api_srt(int argc, char **argv)
{
	URLProtocol *msin_ptl=msptc_api_matchByPtcindex(msptc_srt);
	URLProtocol *msout_ptl=msptc_api_matchByPtcindex(msptc_udp);
	URLContext in_ctt;
	URLContext out_ctt;
	ms_memset(&in_ctt,0,sizeof(URLContext));
	ms_memset(&out_ctt,0,sizeof(URLContext));
	ms_strcpy(in_ctt.url, "srt://127.0.0.1:9000");
	ms_strcpy(out_ctt.url, "udp://192.168.200.244:6000");
	out_ctt.opt.udp.ifcOut="lo";
	out_ctt.opt.udp.ipOut="127.0.0.1";
	out_ctt.flags|=FLAG_WRITE;
	if(msin_ptl->url_open(&in_ctt)<0){
		ms_errExit(-1, "Open in url failed");
	}
	
	out_ctt.opt.srt.listen=1;
	if(msout_ptl->url_open(&out_ctt)<0){
		ms_errExit(-1, "Open out url failed");
	}
	char buf[2048]={0};
	int len=0;
	while(1){
		len=msin_ptl->url_read(&in_ctt,buf,1316);
		
		msout_ptl->url_write(&out_ctt,buf,len);
	}	
	msin_ptl->url_close(&in_ctt);
	msout_ptl->url_close(&out_ctt);
}
int msprotocol_api_igmp(int argc, char **argv)
{
	ms_byte  outbuf_igmp[2048]={0};
	ETHERContext ether_ctt_igmp;
	ms_stru0(&ether_ctt_igmp, ETHERContext);
	ether_ctt_igmp.debug=ms_true;

	MSBULDIGMPContext igmp_ctt_in;
	ms_stru0(&igmp_ctt_in, MSBULDIGMPContext);
	igmp_ctt_in.str_source_mac	="b8:ca:3a:b7:72:65";
	igmp_ctt_in.str_dest_mac	="01:00:5e:02:02:02";
	igmp_ctt_in.source_ip		="192.168.0.83";
	igmp_ctt_in.dest_ip			="224.2.2.3";

		
	URLProtocol *pffurl_raw=(URLProtocol *)msptc_api_matchByPtcindex(msptc_raw);
	URLContext rtpraw_urlctt;
	rtpraw_urlctt.ifcOut="enp3s0";
	rtpraw_urlctt.flags=FLAG_WRITE;

	pffurl_raw->url_open(&rtpraw_urlctt);

	igmp_ctt_in.igmp_ver		=IGMP_VERSION_1;
	igmp_ctt_in.igmp_cmd		=IGMP_CMD_REPORT;	//0-query;1-report;2-leave_group
	ms_s32 len=msframe_api_buildIgmp(&ether_ctt_igmp,&igmp_ctt_in,ms_false,outbuf_igmp);
	pffurl_raw->url_write(&rtpraw_urlctt,outbuf_igmp,len);
	igmp_ctt_in.igmp_ver		=IGMP_VERSION_2;
	igmp_ctt_in.igmp_cmd		=IGMP_CMD_REPORT;	//0-query;1-report;2-leave_group
	len=msframe_api_buildIgmp(&ether_ctt_igmp,&igmp_ctt_in,ms_false,outbuf_igmp);
	pffurl_raw->url_write(&rtpraw_urlctt,outbuf_igmp,len);

	igmp_ctt_in.igmp_ver		=IGMP_VERSION_2;
	igmp_ctt_in.igmp_cmd		=IGMP_CMD_LEAVE_GROUP;	//0-query;1-report;2-leave_group
	len=msframe_api_buildIgmp(&ether_ctt_igmp,&igmp_ctt_in,ms_false,outbuf_igmp);
	pffurl_raw->url_write(&rtpraw_urlctt,outbuf_igmp,len);
	igmp_ctt_in.igmp_ver		=IGMP_VERSION_3;
	igmp_ctt_in.igmp_cmd		=IGMP_CMD_REPORT;	//0-query;1-report;2-leave_group
	len=msframe_api_buildIgmp(&ether_ctt_igmp,&igmp_ctt_in,ms_false,outbuf_igmp);
	pffurl_raw->url_write(&rtpraw_urlctt,outbuf_igmp,len);

	igmp_ctt_in.igmp_ver		=IGMP_VERSION_3;
	igmp_ctt_in.igmp_cmd		=IGMP_CMD_LEAVE_GROUP;	//0-query;1-report;2-leave_group
	len=msframe_api_buildIgmp(&ether_ctt_igmp,&igmp_ctt_in,ms_false,outbuf_igmp);
	pffurl_raw->url_write(&rtpraw_urlctt,outbuf_igmp,len);
	
	pffurl_raw->url_close(&rtpraw_urlctt);

	//ms_bufHex("igmp", outbuf_igmp, len);

}
int main(int argc, char **argv)
{
	mslog_api_init( 52, ms_null,ms_null, ms_null);
	msthread_api_envInit();
	msmem_api_init(ms_false);
	msptc_api_init();
	msptc_api_register();

	msprotocol_api_mctu( argc,  argv);
	//msprotocol_api_igmp( argc,  argv);
	
end:	
	msthread_api_envDeinit();
	msptc_api_deinit();
	msmem_api_deinit();
	mslog_api_deinit();
}

#undef MAIN_C

